/*
 * @Author: H.
 * @Date: 2021-11-29 09:01:53
 * @LastEditTime: 2021-12-06 18:56:43
 * @Description:
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'
import { firstMenu } from '@/utils/map-menus'
const routes: Array<RouteRecordRaw> = [
  { path: '/', redirect: '/main' },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/index.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/not-found/index.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 路由守卫
router.beforeEach((to) => {
  if (to.path !== '/login') {
    // 判断用户是否登录
    const token = localCache.getCache('token')
    if (!token) {
      return '/login'
    }
    // 访问首页时前往菜单栏第一项
    if (to.path === '/main') {
      return firstMenu.url
    }
  }
})
export default router
